<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="人员名称" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入人员名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="resourceList" >
      <el-table-column label="记录单号" prop="id" width="120" align="center" fixed="left" />
      <el-table-column label="申报单号" prop="reportId" width="120"  align="center" />
      <el-table-column label="是否完成" prop="status" width="120"  align="center" />
      <el-table-column label="维保记录" prop="content" width="320"  align="center" />
      <el-table-column label="上传照片" prop="image" align="center" :width="imageColWidth">
        <template v-slot:default="scope">
          <image-preview v-for="(item,index) in scope.row.images"
            style="width: 100px; height: 100px; margin-right: 15px"
            :src="item">
          </image-preview>
        </template>
      </el-table-column>
      <el-table-column label="记录人" prop="createBy" width="120" align="center"/>
      <el-table-column label="修改人" prop="updateBy" width="120" align="center"/>
      <el-table-column label="记录日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="160">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改食材配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="申报单号" prop="reportId">
            <el-input  v-model="form.reportId" placeholder="请输入申报单号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="是否完成" prop="status">
            <el-select v-model="form.status"  clearable placeholder="请选择状态">
              <el-option label="完成" value="完成"></el-option>
              <el-option label="未完成" value="未完成"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="维保记录" prop="content">
        <el-input type="textarea" v-model="form.content" placeholder="请输入维保记录" />
      </el-form-item>
      <el-form-item label="上传图片" prop="image" required>
        <ImageUpload
          v-model="form.image"
          :limit="3"/>
      </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAll, list, get, del, add, update } from "@/api/device/record";
export default {
  name: "Resource",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      id: undefined,
      imageColWidth: 150,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 食材表格数据
      resourceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined
      },
      // 表单参数
      form: {
        content: "",
        image: "",
        status: "",
        reportId: "",

      },
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.id = this.$route.params && this.$route.params.id;
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      list({
        reportId: this.id,
      }).then(response => {
        this.resourceList = response.rows;
        let maxWith = 1;
        for (var i = 0; i < response.rows.length; i++) {
          response.rows[i].images = [];
          if (response.rows[i].image != null && response.rows[i].image != undefined) {
            response.rows[i].images = response.rows[i].image.split(",");
          }
          if (response.rows[i].images.length > maxWith) {
            maxWith = response.rows[i].images.length;
          }
        }
        this.imageColWidth = maxWith * 150;
        this.total = response.total;
        this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form = {};
      this.open = true;
      this.title = "添加维保记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      get(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改维保记录";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.reportId =this.id
          if (this.form.id != undefined) {
            update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const roleIds = row.id || this.ids;
      this.$modal.confirm('是否确认删除角色编号为"' + roleIds + '"的数据项？').then(function() {
        return del(roleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
